import { IApiRes, GoodsInfo } from "@/types/data";
import request from "@/utils/request";
import { defineStore } from "pinia";


export default defineStore('goods', {
  state: () => {
    return {
      info: {} as GoodsInfo
    }
  },
  actions: {
    async getGoodsInfo (id: string) {
      const res = await request.get<IApiRes<GoodsInfo>>('/goods', {params: {id }})
      console.log(res)
      this.info = res.data.result
    },
    // 更新商品规格
    updataPrice(skuId: string) {
      const sku = this.info.skus.find(item => item.id === skuId)
      if (sku) {
        this.info.price = sku.price
        this.info.oldPrice = sku.oldPrice
        this.info.inventory = sku.inventory
      }
    }
  }
})